package com.hyk.commonLib.common.entity;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import androidx.annotation.DrawableRes;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieCompositionFactory;
import com.airbnb.lottie.LottieDrawable;
import com.hyk.commonLib.common.utils.AppUtils;
import com.hyk.commonLib.common.utils.HashUtils;
import com.hyk.commonLib.common.utils.ResUtils;

import java.io.File;

import lombok.Getter;
import lombok.Setter;

@Getter
public final class ImageSource {
    private String imgUrl;
    private File imgFile;
    private Drawable imgDrawable;
    private Bitmap imgBitmap;
    @DrawableRes
    private int imgDrawableRes;
    private LottieRes lottieRes;

    private ImageSource() {
    }

    public boolean hasUrl() {
        return imgUrl != null;
    }

    public boolean hasFile() {
        return imgFile != null;
    }

    public boolean hasDrawable() {
        return imgDrawable != null;
    }

    public boolean hasBitmap() {
        return imgBitmap != null;
    }

    public boolean hasDrawableRes() {
        return imgDrawableRes != 0;
    }

    public boolean hasLottieRes() {
        return lottieRes != null;
    }

    public boolean notEmpty() {
        return hasUrl() || hasFile() || hasDrawable() || hasBitmap() || hasDrawableRes() || hasLottieRes();
    }

    public boolean canConvertToDrawable() {
        return hasFile() || hasDrawable() || hasBitmap() || hasDrawableRes() || hasLottieRes();
    }

    public Drawable convertToDrawable() {
        if (hasDrawable()) {
            return getImgDrawable();
        } else if (hasDrawableRes()) {
            return ResUtils.getDrawable(getImgDrawableRes());
        } else if (hasFile()) {
            return Drawable.createFromPath(getImgFile().getAbsolutePath());
        } else if (hasBitmap()) {
            return new BitmapDrawable(ResUtils.getResource(), imgBitmap);
        } else if (hasLottieRes()) {
            return lottieRes.getDrawable();
        }
        return null;
    }

    // region 静态获取

    public static ImageSource fromUrl(String url) {
        ImageSource imageSource = new ImageSource();
        imageSource.imgUrl = url;
        return imageSource;
    }

    public static ImageSource fromFile(File file) {
        ImageSource imageSource = new ImageSource();
        imageSource.imgFile = file;
        return imageSource;
    }

    public static ImageSource fromFile(String filePath) {
        return fromFile(new File(filePath));
    }

    public static ImageSource fromDrawable(Drawable drawable) {
        ImageSource imageSource = new ImageSource();
        imageSource.imgDrawable = drawable;
        return imageSource;
    }

    public static ImageSource fromBitmap(Bitmap bitmap) {
        ImageSource imageSource = new ImageSource();
        imageSource.imgBitmap = bitmap;
        return imageSource;
    }

    public static ImageSource fromDrawableRes(@DrawableRes int drawableRes) {
        ImageSource imageSource = new ImageSource();
        imageSource.imgDrawableRes = drawableRes;
        return imageSource;
    }

    public static ImageSource fromLottie(LottieRes lottieRes) {
        ImageSource imageSource = new ImageSource();
        imageSource.lottieRes = lottieRes;
        return imageSource;
    }

    // endregion


    public static class LottieRes {
        private String assetFileName;
        private String url;
        private String jsonString;

        @LottieDrawable.RepeatMode
        @Setter(onMethod_ = {@LottieDrawable.RepeatMode})
        private int repeatMode = LottieDrawable.RESTART;
        @Setter
        private int repeatCount = LottieDrawable.INFINITE;


        public static LottieRes fromAssetFileName(String assetFileName) {
            LottieRes lottieRes = new LottieRes();
            lottieRes.assetFileName = assetFileName;
            return lottieRes;
        }

        public static LottieRes fromUrl(String url) {
            LottieRes lottieRes = new LottieRes();
            lottieRes.url = url;
            return lottieRes;
        }

        public static LottieRes fromJsonString(String jsonString) {
            LottieRes lottieRes = new LottieRes();
            lottieRes.jsonString = jsonString;
            return lottieRes;
        }

        public LottieRes repeatInfinite() {
            repeatCount = LottieDrawable.INFINITE;
            return this;
        }

        public LottieRes repeatNone() {
            repeatCount = 0;
            return this;
        }

        public Drawable getDrawable() {
            LottieDrawable drawable = new LottieDrawable();
            if (!TextUtils.isEmpty(assetFileName)) {
                drawable.setComposition(LottieCompositionFactory.fromAssetSync(AppUtils.getAppContext(), assetFileName).getValue());
                drawable.setRepeatMode(repeatMode);
                drawable.setRepeatCount(repeatCount);
            } else if (!TextUtils.isEmpty(url)) {
                LottieCompositionFactory.fromUrl(AppUtils.getAppContext(), url, HashUtils.MD5(url))
                        .addListener(drawable::setComposition);
                drawable.setRepeatMode(repeatMode);
                drawable.setRepeatCount(repeatCount);
            } else if (!TextUtils.isEmpty(jsonString)) {
                drawable.setComposition(LottieCompositionFactory.fromJsonStringSync(jsonString, HashUtils.MD5(jsonString)).getValue());
                drawable.setRepeatMode(repeatMode);
                drawable.setRepeatCount(repeatCount);
            }
            return drawable;
        }

        public void loadAnim(LottieAnimationView imageView) {
            if (!TextUtils.isEmpty(assetFileName)) {
                imageView.setAnimation(assetFileName);
                imageView.setRepeatMode(repeatMode);
                imageView.setRepeatCount(repeatCount);
                imageView.playAnimation();
            } else if (!TextUtils.isEmpty(url)) {
                imageView.setAnimationFromUrl(url);
                imageView.setRepeatMode(repeatMode);
                imageView.setRepeatCount(repeatCount);
                imageView.playAnimation();
            } else if (!TextUtils.isEmpty(jsonString)) {
                imageView.setAnimationFromJson(jsonString, HashUtils.MD5(jsonString));
                imageView.setRepeatMode(repeatMode);
                imageView.setRepeatCount(repeatCount);
                imageView.playAnimation();
            }
        }
    }
}
